//
//  MyDeviceListViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/23.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "MyDeviceListViewController.h"
#import "DeviceListCell.h"
#import "DeviceViewModel.h"
#import "BindDeviceViewController.h"
#import "ScanCodeManager.h"
#import "ScanBindDeviceViewController.h"
@interface MyDeviceListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)DeviceViewModel *deviceViewModel;
@property(nonatomic,strong)NSMutableArray *deviceList;
@end

@implementation MyDeviceListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"我的设备";
    [self setNavRightBtnForImage:[UIImage imageNamed:@"icon_add"]];
    [self configTableView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self requestData];
}
-(void)requestData{
    [self.deviceViewModel fetchDeviceList:[LoginTools sharedTools].userId cmp:^(NSMutableArray * _Nonnull deviceList) {
        [self.deviceList removeAllObjects];
        [self.deviceList addObjectsFromArray:deviceList];
        [self.mainTableView reloadData];
    }];
}
-(void)rightbuttonPressed:(id)sender{
//    BindDeviceViewController *bindDevice = [[BindDeviceViewController alloc]init];
//    [self.rt_navigationController pushViewController:bindDevice animated:YES complete:nil];
    [[ScanCodeManager shardManager]showScanCode:^(NSString * _Nonnull sn) {
        ScanBindDeviceViewController *scanBindDevice = [[ScanBindDeviceViewController alloc]init];
        scanBindDevice.snCode = sn;
        [self.rt_navigationController pushViewController:scanBindDevice animated:YES complete:nil];
    } fail:^{
        [self.rt_navigationController popViewControllerAnimated:YES complete:nil];
    }];
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"DeviceListCell" bundle:nil] forCellReuseIdentifier:@"DeviceListCell"];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"DeviceListCell" bundle:nil] forCellReuseIdentifier:@"DeviceListCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.deviceList count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DeviceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DeviceListCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    if (indexPath.row==0) {
        [cell.bgView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
    }else if (indexPath.row<2-1){
        [cell.bgView setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
    }else{
        [cell.bgView setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
    }
    [cell setCellValue:[self.deviceList objectAtIndex:indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否要解绑此设备" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        DeviceListModel *deviceModel = [self.deviceList objectAtIndex:indexPath.row];
        [self.deviceViewModel unbindDevice:[NSString stringWithFormat:@"%.0f",deviceModel.internalBaseClassIdentifier] cmp:^{
            [self requestData];
        }];
    }];
    UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:alertAction1];
    [alert addAction:alertAction2];
    [self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - instance
-(DeviceViewModel*)deviceViewModel{
    if (!_deviceViewModel) {
        _deviceViewModel = [[DeviceViewModel alloc]init];
    }
    return _deviceViewModel;
}
-(NSMutableArray*)deviceList{
    if (!_deviceList) {
        _deviceList = [[NSMutableArray alloc]init];
    }
    return _deviceList;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
